--
-- Author: Xiangkui Guo
-- Date: 2017-10-20
--
-- MainView
-- 战斗UI根节点

BattleUIConst =
{
    BuffMax = 8,
}

--缓存管理 begin
MainBattleUICache = MainBattleUICache

if not MainBattleUICache then
    --data cache
    MainBattleUICache = {
        objs = {},
        find = function(tb,name)
            local obj = tb.objs[name]
            if not obj then
                local uiLayout = cc.CSLoader:createNode(name,false,true)
                uiLayout:retain()
                obj = uiLayout
                tb.objs[name] = uiLayout
            end
            if obj:getParent() then
                obj:removeFromParent()
            end
            return obj
        end
    }

    --regist dispose objs
    EM:on("OnGameSceneExit", function()
        for name,obj in pairs(MainBattleUICache.objs) do
--            print("OnGameSceneExit",name,obj:getReferenceCount())
            obj:removeAllChildren()
            obj:release()
        end
        for key,_ in pairs(MainBattleUICache.objs) do
            MainBattleUICache.objs[key] = nil
        end
    end)
end
--缓存管理 end

--MClass方法 作为节点管理
MClass = MClass or require("app.battleV3.ui.MClass")
--战斗UI事件管理  设置到全局
BattleUIEvent = require("app.battleV3.ui.EventMgr").new()
local TYPES = {
    --SHOW UI
    UI_SHOW         =   "UI_SHOW",
    MVP_SHOW        =   "MVP_SHOW",
    QC_SHOW         =   "QC_SHOW",
    QC_SHOW_BTN     =   "QC_SHOW_BTN",
    --PLAYER NODE
    PLAYER_ADD      =   "PLAYER_ADD",
	--PLAYER
	PLAYER_MOVE 	=	"PLAYER_MOVE",
    PLAYER_ROLL     =   "PLAYER_ROLL",
    PLAYER_TALK     =   "PLAYER_TALK",
	--BATTLE
	BATTLE_TIME 	=	"BATTLE_TIME",
	BATTLE_ESCAPE 	=	"BATTLE_ESCAPE",
	BATTLE_SET 		=	"BATTLE_SET",
    BATTLE_SET_ABLE =   "BATTLE_SET_ABLE",
	BATTLE_AUTO 	=	"BATTLE_AUTO",
    BATTLE_CANCEL_AUTO     =   "BATTLE_CANCEL_AUTO",
    BATTLE_FMTION   =   "BATTLE_FMTION",
    BATTLE_KEEPBKIL =   "BATTLE_KEEPBKIL",

    BATTLE_PVP_INIT_TOP   =   "BATTLE_PVP_INIT_TOP",
    BATTLE_PVP_FEFRESH_TOP   =   "BATTLE_PVP_FEFRESH_TOP",
    BATTLE_SHOW_PVP_REVIVE = "BATTLE_SHOW_PVP_REVIVE",

    --fish
    BATTLE_FISH_INIT_TOP = "BATTLE_FISH_INIT_TOP",

    --kof
    BATTLE_SHOW_KOF_UI = "BATTLE_SHOW_KOF_UI",
    BATTLE_KOF_UPDATE_DATA = "BATTLE_KOF_UPDATE_DATA",
    BATTLE_KOF_UI_HEAD_INIT = "BATTLE_KOF_UI_HEAD_INIT",
    BATTLE_KOF_HP_UPDATE = "BATTLE_KOF_HP_UPDATE",
    BATTLE_KOF_HP_HIDE = "BATTLE_KOF_HP_HIDE",
    SHOW_BLACK_SCREEN = "SHOW_BLACK_SCREEN",

    BATTLE_SHOW_BUFF_BLIND = "BATTLE_SHOW_BUFF_BLIND",

    BATTLE_REVIVE 	=	"BATTLE_REVIVE",

	--SKILL
    SKILL_ATTACK    =   "SKILL_ATTACK",
	SKILL_SHOWTIPS	=	"SKILL_SHOWTIPS",
	SKILL_HIDETIPS	=	"SKILL_HIDETIPS",
	SKILL_SELECT	=	"SKILL_SELECT",
    SKILL_FINISH_CHANT_CHARGE = "SKILL_FINISH_CHANT_CHARGE",
	SKILL_INIT		=	"SKILL_INIT",
	SKILL_UPDATE	=	"SKILL_UPDATE",
    SKILL_COMBO     =   "SKILL_COMBO",
    SKILL_EFFECT    =   "SKILL_EFFECT",
    SKILL_HIDEEFFECT=   "SKILL_HIDEEFFECT",
    SKILL_RLSEFFECT =   "SKILL_RLSEFFECT",
    SKILL_PROCESS   =   "SKILL_PROCESS",
    SKILL_ROLLCD    =   "SKILL_ROLLCD",
    SKILL_ROLLCD_HINT = "SKILL_ROLLCD_HINT",
    SKILL_SETCOST   =   "SKILL_SETCOST",
    SKILL_COREBONUS =   "SKILL_COREBONUS",
    SKILL_SHOW_REPLACE_BTN  =   "SKILL_SHOW_REPLACE_BTN", --按的时候touchbegan浮现更大替换按钮
    SKILL_HIDE_REPLACE_BTN  =   "SKILL_HIDE_REPLACE_BTN",
    SKILL_SHOW_XULI_BTN = "SKILL_SHOW_XULI_BTN",
    SKILL_HIDE_XULI_BTN = "SKILL_HIDE_XULI_BTN",
    --TAB
    TARGT_TAB       =   "TARGT_TAB",

    -- 战斗商店
    SHOW_BATTLE_SHOP =   "SHOW_BATTLE_SHOP",
    HIDE_BATTLE_SHOP =   "HIDE_BATTLE_SHOP",

    --Melee Battle
    MELEE_LAYOUT    =   "MELEE_LAYOUT",
    FISH_LAYOUT     =   "FISH_LAYOUT",
    MELEE_MAP       =   "MELEE_MAP",
    SHOW_MELEE_MAP  =   "SHOW_MELEE_MAP",
    SHOW_MELEE_RANK =   "SHOW_MELEE_RANK",
    BUY_ITEM        =   "BUY_ITEM",
    USE_ITEM        =   "USE_ITEM",
    MELEE_PROP      =   "MELEE_PROP",
    MELEE_SKILL     =   "MELEE_SKILL",
    MELEE_SCORE     =   "MELEE_SCORE",
    MELEE_FOCUS     =   "MELEE_FOCUS",
    MELEE_STATUS    =   "MELEE_STATUS",
    MELEE_DEADCD    =   "MELEE_DEADCD",
    SHOW_KILL_TIPS  =   "SHOW_KILL_TIPS",

    --Zombie battle
    BATTLE_REFRESH_ROUND    =   "BATTLE_REFRESH_ROUND",                 --刷新当前回合数
	BATTLE_REFRESH_FLOWER_NODE = "BATTLE_REFRESH_FLOWER_NODE",			-- 刷新月影花
	BATTLE_REFRESH_ZOMBIE_SHOP_TIPS = "BATTLE_REFRESH_ZOMBIE_SHOP_TIPS", -- 刷新鱼人商店TIPS

    --狮心号角
    SHOW_LION_KILL_LIST = "SHOW_LION_KILL_LIST",
    BATTLE_REFRESH_KILLLIST = "BATTLE_REFRESH_KILLLIST",

    --TARGT
    TARGT_SETDATA   =   "TARGT_SETDATA",
    TARGT_SETDATA2  =   "TARGT_SETDATA2",
    TARGT_UPDATE    =   "TARGT_UPDATE",
    TARGT_LOCK      =   "TARGT_LOCK",
    TARGT_TEAM_MARK =   "TARGT_TEAM_MARK",

    --STATUS
    STATUS_ID       =   "STATUS_ID",
    STATUS_MANA     =   "STATUS_MANA",
    STATUS_HP       =   "STATUS_HP",
    STATUS_HP_HIDE  =   "STATUS_HP_HIDE",

    --BUFF
    BUFF_SELT       =   "BUFF_SELT",
    BUFF_UPDATE     =   "BUFF_UPDATE",

    --Escape
    ESCAPE_SHOWUI   =   "ESCAPE_SHOWUI",
    --Unit交互
    REALTIME_INTERACTION = "REALTIME_INTERACTION",

    BATTLE_UPDATE_MONEY = "BATTLE_UPDATE_MONEY",
    --训练场伤害刷新
    BATTLE_TRAINING_DAMAGE_REFRESH = "BATTLE_TRAINING_DAMAGE_REFRESH",
    BATTLE_TRAINING_SEND_DAMAGE = "BATTLE_TRAINING_SEND_DAMAGE",
    --显示、隐藏leftView/rightView
    BATTLE_SHOW_HIDE_LEFTRIGHT_VIEW = "BATTLE_SHOW_HIDE_LEFTRIGHT_VIEW",
    TRAINING_BATTLE_ESCAPE 	=	"TRAINING_BATTLE_ESCAPE",
}
BattleUIEvent:setEventTypes(TYPES)

local Shop = require("app.battleV3.ui.views.Melee.MobaShop")
local Rank = require("app.battleV3.ui.views.Melee.MobaRank")
local KillTips = require("app.battleV3.ui.objs.KillTips")
local ShopBtnView = require("app.battleV3.ui.objs.ShopButton")
local NodePool = custom.NodePool:getInstance()

--Battle Main View
local BattleUIView = class("BattleUIView",function()
    return display.newLayer("BattleUIView")
end)

local MVP = require("app.battleV3.ui.MVPView")

local leftViewConfig =
{
    name = "leftView",
    res = "player_scroll_1",
    mgr = "LeftView",
}

local rightViewConfig =
{
    name = "rightView",
    res = "player_scroll_2",
    mgr = "RightView",
}

local topViewConfig =
{
    name = "topView",
    res = "top",
    mgr = "TopView",
}

local bottomViewConfig =
{
    name = "bottomView",
    res = "battle_bom_node",
    mgr = "BottomView",
}

local reviveViewConfig =
{
    name = "reviveView",
    mgr = "ReviveView",
    ResName = "BattleReviveSureNode", -- Res中资源名字
}

local mobaTopViewConfig =
{
    name = "mobaTopView",
    res = "moba_top",
    mgr = "Melee.MobaTopView",
}

local mobaBottomViewConfig =
{
    name = "mobaBottomView",
    res = "moba_bom",
    mgr = "Melee.MobaBottomView",
}

local mobaMapViewConfig =
{
    name = "mobaMapView",
    res = "moba_map",
    mgr = "Melee.MobaMapView",
}

local fishTopViewConfig =
{
    name = "fishTopView",
    res = "fish_top",
    mgr = "Melee.FishTopView",
}

local switchViewConfig = {
    name = "SwitchView",
    res = "Node_training_1",
    mgr = "SwitchView",
}

local dpsViewConfig = {
    name = "DPSView",
    res = "Node_training_2",
    mgr = "DPSView",
}

--local UI_Config = {
--        {name = "leftView"  , res = "player_scroll_1", mgr = "LeftView"},
--        {name = "rightView" , res = "player_scroll_2", mgr = "RightView"},
--        {name = "bottomView", res = "battle_bom_node", mgr = "BottomView"},
--		  {name = "topView"	, res = "top", mgr = "TopView"},
--        {name = "reviveView", res = "jjc_bom", mgr = "ReviveView"},
--        {name = "mobaTopView", res = "moba_top", mgr = "Melee.MobaTopView"},
--        {name = "mobaMapView", res = "moba_map", mgr = "Melee.MobaMapView"},
--        {name = "mobaBottomView", res = "moba_bom", mgr = "Melee.MobaBottomView"},
--        {name = "fishTopView", res = "fish_top", mgr = "Melee.FishTopView"}
--}

local UIConfig =
{
    [0] = {leftViewConfig, rightViewConfig, topViewConfig, bottomViewConfig,},
    [1] = {leftViewConfig, rightViewConfig, topViewConfig, bottomViewConfig, reviveViewConfig,},
    [2] = {leftViewConfig, rightViewConfig, bottomViewConfig,},
    [3] = {leftViewConfig, rightViewConfig, mobaTopViewConfig, bottomViewConfig, mobaBottomViewConfig, mobaMapViewConfig,},	--赤道大乱斗
    [301] = {leftViewConfig, rightViewConfig, topViewConfig, bottomViewConfig,},
    [302] = {leftViewConfig, rightViewConfig, fishTopViewConfig, bottomViewConfig, mobaMapViewConfig,},						--鱼人大战僵尸
    [306] = {leftViewConfig, rightViewConfig, topViewConfig, bottomViewConfig, reviveViewConfig,},
    [308] = {leftViewConfig, rightViewConfig, topViewConfig, bottomViewConfig, reviveViewConfig,}, --决斗赛
    [904] = {leftViewConfig, rightViewConfig, topViewConfig, bottomViewConfig,},
    [307] = {leftViewConfig, rightViewConfig, bottomViewConfig, topViewConfig, switchViewConfig, dpsViewConfig,},  --训练场
}

function BattleUIView:ctor(battleType)
    self.battleType = battleType
    NodePool:clear()
    --ui root
--    self.mUINode = cc.CSLoader:createNode(Res.BattleUINew,false,true)
    self.mUINode = MainBattleUICache:find(Res.BattleUINew)
    self.mUINode:setName("BattleScene")
    self.mUINode:setContentSize(cc.size(display.width, display.height))
    display.doLayout(self.mUINode)
    --child nodes
    local config = UIConfig[battleType] or UIConfig[0]
    for i=1, #config do
    	local cfg = config[i]
    	local view
        if cfg.res and "" ~= cfg.res then -- BattleScene UI内部节点
            view = self.mUINode:findChild(cfg.res)
        else -- 外部节点
            view = cc.CSLoader:createNode( Res[cfg.ResName] )
        end
        assert( view, " ============>>>>>>> view不存在？" .. cfg.mgr ) --@ignore
    	local mgr = require("app.battleV3.ui.views."..cfg.mgr)
    	self[cfg.name] = mgr.new(view, battleType)
    end

    self.mBossFloatText = self.mUINode:findChild("FloatText_Boss")

    self.controller = require("app.battleV3.ui.objs.Controller").new(self.mUINode:findChild("controller"))
    self.quickChat  = require("app.battleV3.ui.objs.QuickChat").new(self.mUINode:findChild("chat_tips"))

    self:addChild(self.mUINode)

    self.shopBtn = nil
    -- add init
    self:init()

    self:enableNodeEvents()

    self.killTips = {}
end

function BattleUIView:addShopButton()
    if self.battleType == Enums.EBattleType.BtType_Zombie
      or self.battleType == Enums.EBattleType.BtType_Melee then
        self.shopBtnPos = self.mUINode:findChild("shop")
        if self.shopBtn == nil then
            self.shopBtn = ShopBtnView.new(cc.CSLoader:createNode(Res.ZombieShop,false,true))
            self.shopBtnPos:addChild(self.shopBtn.root)
        end
    end
end

function BattleUIView:removeShopButton()
    if not cc.isDead(self.shopBtn) then
        self.shopBtn:removeFromParent()
        self.shopBtn = nil
    end
end


function BattleUIView:getBossFloatTextNode()
    return self.mBossFloatText
end

function BattleUIView:init()
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.PLAYER_ADD,handler(self,BattleUIView.addPlayerNode))  --两边头像
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.UI_SHOW,handler(self,BattleUIView.showUI))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MVP_SHOW,handler(self,BattleUIView.showMVPUI))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_SHOW_PVP_REVIVE,handler(self,BattleUIView.showDeadTips))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_SHOW_BUFF_BLIND,handler(self,BattleUIView.showBuffBlind))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_SHOW_KOF_UI,handler(self,BattleUIView.showKofUI))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MELEE_LAYOUT,handler(self,BattleUIView.setMeleeLayout))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.FISH_LAYOUT,handler(self,BattleUIView.setFishLayout))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SHOW_BATTLE_SHOP,handler(self,BattleUIView.showBattleShop))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.HIDE_BATTLE_SHOP,handler(self,BattleUIView.hideBattleShop))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SHOW_KILL_TIPS,handler(self,BattleUIView.showKillTips))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SHOW_MELEE_RANK,handler(self,BattleUIView.showMeleeRank))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SHOW_LION_KILL_LIST,handler(self,BattleUIView.showKillListUI))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SHOW_BLACK_SCREEN,handler(self,BattleUIView.showBlackScreen))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_UPDATE_MONEY,handler(self,BattleUIView.updateBattleMoney))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_REFRESH_ROUND,handler(self,BattleUIView.refreshRound))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_SHOW_HIDE_LEFTRIGHT_VIEW,handler(self,BattleUIView.showHideLeftRightView))

    local handle = EM:on("onSettingShowBattleHead", function(name, isShow)
        self:setBothHead()
    end)
    BattleUIEvent:addEmHandle(handle)

    handle = EM:on("onSettingKeepBasicAttack", function(name, isKeep)
        BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_KEEPBKIL,{isKeep = isKeep})
    end)
    BattleUIEvent:addEmHandle(handle)

    handle = EM:on("onSettinAutoBattle", function(name, isAuto)
        BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_AUTO,{isAuto = isAuto})
    end)
    BattleUIEvent:addEmHandle(handle)

    self:setVisible(false)
    self:play("hide",0,true)

    self.bShowUI = false

    self:setBothHead()
    self:setDefault()
end

function BattleUIView:updateBattleMoney(money)
    if self.battleType == Enums.EBattleType.BtType_Zombie
      or self.battleType == Enums.EBattleType.BtType_Melee then
        if self.shopBtn then
            self.shopBtn:updateMoney(money)
        end
    end
end

function BattleUIView:refreshRound(round, time)
    self.fishTopView:showRound(round)
    if not cc.isDead(self.roundTips) then
        self.roundTips:removeFromParent()
        self.roundTips = nil
    end
    local countdownTime = time or Constant.BattleRefreshTipsTimeInAdvance
    --显示怪物刷新波数提示
    local roundTips = cc.CSLoader:createNode(Res.LionTipsNode,false,true)
    if not cc.isDead(roundTips) then
        local labelRound = roundTips:findChild("Sprite/name")
        local labelCountDown = roundTips:findChild("Sprite/tips")
        local strRound = string.format(L("zombieRoundText"), round)
        labelRound:setString(strRound)
        local strRoundCountdown = string.format(L("zombieRefreshCountdownText"), countdownTime)
        labelCountDown:setString(strRoundCountdown)
        self.mBossFloatText:addChild(roundTips)
        local action = cc.Repeat:create(cc.Sequence:create(cc.DelayTime:create(1),
          cc.CallFunc:create(
            function()
                countdownTime = countdownTime - 1
                if countdownTime > 0 then
                    strRoundCountdown = string.format(L("zombieRefreshCountdownText"), countdownTime)
                    labelCountDown:setString(strRoundCountdown)
                else
                    roundTips:removeFromParent()
                    self.roundTips = nil
                end
            end)), time or Constant.BattleRefreshTipsTimeInAdvance)
        roundTips:runAction(action)
        self.roundTips = roundTips
    end
end

function BattleUIView:showBattleShop(shopPageIndex)
    if not self.shop then
        local uiRoot = cc.CSLoader:createNode(Res.BattleShop)
        --display.doLayout(uiRoot)
        uiRoot:setContentSize(display.ui_width, display.ui_height)
        uiRoot:setAnchorPoint(0.5, 0.5)
        uiRoot:setPosition(display.width/2, display.height/2)
        display.setAutoScale(uiRoot)
        ccui.Helper:doLayout(uiRoot)

        local shopObj= Shop.new(uiRoot, self.battleType, shopPageIndex)
        self.shop = shopObj
        UIHandler:addPopupUi(uiRoot, "BattleShop")
        self.shop:show(true)
    else
        self.shop:show(true)
        self.shop:setShopPageIndex(shopPageIndex)
    end
end

function BattleUIView:showKillTips(data)
    local root,mgr = KillTips.create(self)
    self.mBossFloatText:addChild(root)
    mgr:setData(data)
end

function BattleUIView:hideBattleShop()
    if self.shop then
        self.shop:show(false)
    end
    if self.shopBtn then
        self.shopBtn:setVisible(false)
    end
end

function BattleUIView:showMeleeRank()
    if not self.rank then
        local rankObj= Rank.new(cc.CSLoader:createNode(Res.MeleeRank,false,true))
        self.rank = rankObj
        self:addChild(rankObj.root)
    else
        self.rank:setVisible(true)
    end
end

function BattleUIView:setMeleeLayout()
    self.mUINode:findChild("top"):setVisible(false)
    self.mUINode:findChild("battle_bom_node"):setVisible(true)
    self.mUINode:findChild("moba_top"):setVisible(true)
    self.mUINode:findChild("moba_map"):setVisible(true)
    self.mUINode:findChild("moba_bom"):setVisible(true)
    self.mUINode:findChild("fish_top"):setVisible(false)
    self.mobaMapView:initTouch()
    if not EFFECT_ALL_SHOW then
        self.mobaMapView:hide()
    end
    self.bottomView:setMeleeLayout()
    self:addShopButton()
end

function BattleUIView:setFishLayout()
    self.mUINode:findChild("top"):setVisible(false)
    self.mUINode:findChild("battle_bom_node"):setVisible(true)
    self.mUINode:findChild("moba_top"):setVisible(false)
    self.mUINode:findChild("moba_map"):setVisible(true)
    self.mUINode:findChild("moba_bom"):setVisible(false)
    self.mUINode:findChild("fish_top"):setVisible(true)
    self.mobaMapView:initTouch()
    if not EFFECT_ALL_SHOW then
        self.mobaMapView:hide()
    end
    self:addShopButton()
end

function BattleUIView:setDefault()
    if not app:isBattleGuideComp() then
        -- 新手战初始化
        self.mUINode:findChild("top"):setVisible( false )
        self.controller:setVisible( true )
        self.leftView:setVisible( false )
        self.rightView:setVisible( false )
        self.bottomView.skillPanelView.attack:setVisible(false)
    else
        self.mUINode:findChild("top"):setVisible(true)
        self.controller:setVisible( true )
        self.leftView:setVisible( true )
        self.rightView:setVisible( true )
    end

    self.mUINode:findChild("battle_bom_node"):setVisible(true)
    self.mUINode:findChild("moba_top"):setVisible(false)
    self.mUINode:findChild("moba_map"):setVisible(false)
    self.mUINode:findChild("moba_bom"):setVisible(false)
    self.mUINode:findChild("fish_top"):setVisible(false)
end

function BattleUIView:play(actName,_timeScale,bStart)
    -- body
    local timeScale = _timeScale or 1
    if actName == "appear" then
        self:setVisible(true)
        self.leftView:runAction(cca.moveBy(0.2*timeScale, 300,0))
        self.rightView:runAction(cca.moveBy(0.2*timeScale, -300, 0))
        if self.topView then
            self.topView:runAction(cca.moveBy(0.2*timeScale, 0, -150))
        end
        if self.bottomView then
            self.bottomView:runAction(cca.moveBy(0.2*timeScale, 0, 300))
        end
        if self.mobaTopView then
            self.mobaTopView:runAction(cca.moveBy(0.2*timeScale, 0, -150))
        end
        if self.mobaBottomView then
            self.mobaBottomView:runAction(cca.moveBy(0.2*timeScale, 0, 300))
        end
        if self.switchView then
            self.switchView:runAction(cca.moveBy(0.2*timeScale, 300,0))
        end
        if self.dpsView then
            self.dpsView:runAction(cca.moveBy(0.2*timeScale, -300, 0))
        end
        self.controller:runAction(cca.moveBy(0.2*timeScale, 0, 200))
    elseif actName == "hide" then
        self.leftView:runAction(cca.moveBy(0.2*timeScale, -300, 0))
        self.rightView:runAction(cca.moveBy(0.2*timeScale, 300, 0))
        if self.topView then
            self.topView:runAction(cca.moveBy(0.2*timeScale, 0, 150))
        end
        if self.bottomView then
            self.bottomView:runAction(cca.moveBy(0.2*timeScale, 0, -300))
        end
        if self.mobaTopView then
            self.mobaTopView:runAction(cca.moveBy(0.2*timeScale, 0, 150))
        end
        if self.mobaBottomView then
            self.mobaBottomView:runAction(cca.moveBy(0.2*timeScale, 0, -300))
        end
        if self.mobaMapView then
            self.mobaMapView:setVisible(false)
        end
        if self.switchView then
            self.switchView:runAction(cca.moveBy(0.2*timeScale, -300, 0))
        end
        if self.dpsView then
            self.dpsView:runAction(cca.moveBy(0.2*timeScale, 300, 0))
        end
        self.controller:runAction(cca.moveBy(0.2*timeScale, 0, -200))
        if not bStart then
            self:setVisible(true)
            EM:notify("onSettingShowBattleChat",false)
        else
            self:setVisible(false)
        end
    end
end

function BattleUIView:showMVPUI(name,bOurTeam,player)
    self:showUI(false)
    self:performWithDelay(function()
        self.mvp = MVP.new()
        self:addChild(self.mvp)
        self.mvp:setData(name,bOurTeam,player)
    end,0.5)
end

function BattleUIView:showUI(bShow)
    if self.bShowUI == bShow then
        return
    end
    self.bShowUI = bShow
    if self.bShowUI then
        self:play("appear")
    else
        self:play("hide")
    end
end

function BattleUIView:showKofUI(event)
    local isSelfLeft = event.isSelfLeft
    self.mUINode:findChild("top"):setVisible(false)

    if not self.kofTopView then
        local KofTopView = require("app.battleV3.ui.views.KofTopView")
        self.kofTopView = KofTopView.new(isSelfLeft)
        self.kofTopView:setPosition(display.width/2, 0)
        self:addChild(self.kofTopView)
    end

    --隐藏左右头像
    self.leftView:setVisible(false)
    self.rightView:setVisible(false)
end

function BattleUIView:showHideLeftRightView(aVisible)
    if self.leftView then
        self.leftView:setVisible(aVisible)
    end
    if self.rightView then
        self.rightView:setVisible(aVisible)
    end
end

function BattleUIView:showKillListUI(event)
    if not self.killListView then
        local KillListView = require("app.battleV3.ui.views.KillListView")
        self.killListView = KillListView.new()
        -- self.killListView:setPosition(display.width/2, 0)
        self.mLionHeadNode = self.mLionHeadNode or self.mUINode:findChild("lion_head")
        self.mLionHeadNode:addChild(self.killListView)
    end
end

function BattleUIView:showDeadTips(event)
    self.mPvpDeadTips = self.mPvpDeadTips or self.mUINode:findChild("FloatText_jjc_die")
    local reviveSelfTimes = event.reviveSelfTimes
    if reviveSelfTimes > 0 then
        --显示复活提示
        self.mPvpDeadTips:setVisible(true)

        --隐藏 技能/摇杆
        self.bottomView:setVisible(false)
        self.controller:setVisible(false)
    else
        --隐藏复活提示
        self.mPvpDeadTips:setVisible(false)

        --显示 技能/摇杆
        self.bottomView:setVisible(true)
        self.controller:setVisible(true)
    end
end

function BattleUIView:onExit()
    local tempFunc = cc.isAlive
    NodePool:clear()
    self:setDefault()
    BattleUIEvent:clear()
    self.leftView:clear()
    self.rightView:clear()
    if self.topView then
        self.topView:reset()
    end
    if self.kofTopView then
        self.kofTopView:clearHideEffect()
    end

    if tempFunc(self.winEffect) then
        self.winEffect:removeFromParent()
    end
    if tempFunc(self.loseEffect) then
        self.loseEffect:removeFromParent()
    end

    if tempFunc(self.mUINode) then
        self.mUINode:removeFromParent()
    end

    if tempFunc(self.mvp) then
        self.mvp:removeFromParent()
    end

    if tempFunc(self.shopBtn) then
        self.shopBtn:removeFromParent()
    end

    if tempFunc(self.roundTips) then
        self.roundTips:removeFromParent()
    end

    self:unregisterScriptHandler()

    if tempFunc(self.shop) then
        UIHandler:removePopupUiByKey("BattleShop")
        self.shop:removeFromParent()
    end

    if tempFunc(self.rank) then
        self.rank:removeFromParent()
    end

    if tempFunc(self.killListView) then
        self.killListView:removeFromParent()
    end

    for _, tips in pairs(self.killTips) do
        tips:putToPool()
    end
    if tempFunc(self.DPSView) then
        self.DPSView:clear()
    end
    if tempFunc(self.SwitchView) then
        self.SwitchView:clear()
    end
end

function BattleUIView:getBossFloatTextPos()
    if self.mBossFloatText then
        local x,y = self.mBossFloatText:getPosition()
        local posX,posY = self.mBossFloatText:getParent():toWorldPosition(x,y)
        return posX,posY
    end
    return nil
end

function BattleUIView:setBothHead()
    self.leftView:setVisible(UD.setting:isShowBattleHead())
    self.rightView:setVisible(UD.setting:isShowBattleHead())
end

function BattleUIView:addPlayerNode(isLeft,nodes)
    if isLeft then
        self.leftView:addView(nodes)
    else
        self.rightView:addView(nodes)
    end
end

function BattleUIView:showScreenBlackCDEffect(b)
    if self.screenBlackCDEffect and b and self.screenBlackCDEffect:isVisible() then
        --如果要显示，但是已经显示，则不需要设置
        return
    end

    if self.screenBlackCDEffect == nil then
        local x,y = self.mBossFloatText:getPosition()

        local Armature = app:getClass("Armature")
	    self.screenBlackCDEffect = Armature.create({path=Res.ScreenBlackCDEffect.dir,armatureName=Res.ScreenBlackCDEffect.name,animationName=""})
        self.screenBlackCDEffect:setPosition(x, y)
        self:addChild(self.screenBlackCDEffect)
    end

    -- self.screenBlackCDEffect:setVisible(true)
    if self.screenBlackCDEffect then
        self.screenBlackCDEffect:setVisible(b)
        if b then
            self.screenBlackCDEffect:playAni({name="effcreat",playTimes = 1,completeCallback=function()
                -- self.screenBlackCDEffect:setVisible(false)
            end})
        end
    end
end

function BattleUIView:showBlackScreen(b)
    -- print("=============BattleUIView:showBlackScreen==========")
    if b and self.blackScreenLayer == nil then
        self.blackScreenLayer = display.newColorLayer(cc.c4b(0, 0, 0, 255))
        self:addChild(self.blackScreenLayer)
    end

    if self.blackScreenLayer then
        -- self.blackScreenLayer:setVisible(b)

        if b then
            local action = self.blackScreenLayer:getActionByTag(Enums.CommonActionTag.BattleBlackScreen)
            if not action then
                action = cca.seq({
                    cca.fadeIn(0.6),
                    cca.fadeOut(0.5)
                })
                action:setTag(Enums.CommonActionTag.BattleBlackScreen)
                self.blackScreenLayer:runAction(action)
            end
        end
    end
end

function BattleUIView:showWinEffect(bHide,cb)
   if self.winEffect == nil then
        local Armature = app:getClass("Armature")
	    self.winEffect = Armature.create({path=Res.BattleWinEffect.dir,armatureName=Res.BattleWinEffect.name,animationName=""})
        -- self.winEffect = display.newArmature({path=Res.BattleWinEffect.dir,armatureName=Res.BattleWinEffect.name,animationName="",skeletonName=Res.BattleWinEffect.name})
        -- self.winEffect:getAnimation():play()

        local x = display.width/2.0
        local y = display.height/2.0
        self:addChild(self.winEffect)
        self.winEffect:setPosition(x,y)
    end

    if self.winEffect then
        self.winEffect:setVisible(true)
        self.winEffect:playAni({name=Res.BattleWinEffect.name,playTimes = 1,completeCallback=function()
            cb()
            if cc.isAlive(self.winEffect) then
                self.winEffect:setVisible(not bHide)
            end
        end})
    end
end

function BattleUIView:showLoseEffect(bHide,cb)
   if self.loseEffect == nil then
        local Armature = app:getClass("Armature")
	    self.loseEffect = Armature.create({path=Res.BattleLoseEffect.dir,armatureName=Res.BattleLoseEffect.name,animationName=""})
        -- self.loseEffect = display.newArmature({path=Res.BattleLoseEffect.dir,armatureName=Res.BattleLoseEffect.name,animationName="",skeletonName=Res.BattleLoseEffect.name})
        -- self.loseEffect:getAnimation():play()

        local x = display.width/2.0
        local y = display.height/2.0

        self:addChild(self.loseEffect)
        self.loseEffect:setPosition(x,y)
    end

    if self.loseEffect then
        self.loseEffect:setVisible(true)

        self.loseEffect:playAni({name=Res.BattleLoseEffect.name,playTimes = 1,completeCallback=function()
            cb()
            if cc.isAlive(self.loseEffect) then
                self.loseEffect:setVisible(not bHide)
            end
        end})

    end
end

function BattleUIView:showBuffBlind()
    if not self.blindLayer then
        self.blindLayer = display.newColorLayer(cc.c4b(255, 255, 255, 255))
        self.blindLayer:setContentSize(display.width,display.height)
        self.blindLayer:setVisible(false)
        self:addChild(self.blindLayer, -1)
    end

    self.blindLayer:stopActionByTag(Enums.CommonActionTag.BattleBlindUI)
    self.blindLayer:setVisible(true)
    self.blindLayer:setOpacity(255)
    local actions = {}
    table.insert(actions, cc.FadeOut:create(1.0))
    table.insert(actions, cc.Hide:create())
    local action = cc.Sequence:create(actions)
    action:setTag(Enums.CommonActionTag.BattleBlindUI)
    self.blindLayer:runAction(action)
end

return BattleUIView
